$(function () {
    container.off('click');
    function getMyEquipmentRepairsByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "equipmentName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getMyEquipmentRepairsByName(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getMyEquipmentRepairsByName(0);
    });


    // 撤销待审核的器材入库
    container.on("click","#undo-equipmentRepairs", function () {
        // 获取 CSRF Token
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:'DELETE',
            url: "/equipmentRepair/"+ $(this).attr("teacherNumber")+'/revocation/'+$(this).attr("repairId"),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            if (response.data.success) {
                // 从新刷新主界面
                getMyEquipmentRepairsByName(0);
                toastr.success(response.data.message);
            } else {
                toastr.error(response.data.message);
            }
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    // 撤销待审核的器材入库
    container.on("click","#ok-equipmentRepairs", function () {
        axios({
            method:'GET',
            url: "/equipmentRepair/"+ $(this).attr("teacherNumber")+'/repairOk/'+$(this).attr("repairId")
        }).then(function (response) {
            $("#repairForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //完成器材报修
    $("#repairForm").submit(function () {
        // 获取 CSRF Token
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:"POST",
            url:"/equipmentRepair",
            data:$('#repairForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#repairForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getMyEquipmentRepairsByName(0);
                $("#repairForm").html("");
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });
        $("#repairDialogClose").click();
        return false;
    });

    //关闭器材保报修完成界面
    $("#repairDialogClose").click(function () {
        $("#repairForm").html("");
    });

});